package main

import "strings"

func convert(s string, numRows int) string {
	if numRows == 1 {
		return s
	}
	m := make([][]byte, numRows)
	flag := 0
	for i := 0; i < len(s); {
		if flag == 0 {
			for j := 0; j < numRows && i < len(s); j++ {
				m[j] = append(m[j], s[i])
				i++
			}
		} else {
			k := numRows - 1 - flag
			m[k] = append(m[k], s[i])
			i++
		}
		if flag == numRows-2 {
			flag = 0
		} else {
			flag++
		}
	}
	ret := strings.Builder{}
	for _, row := range m {
		ret.Write(row)
	}
	return ret.String()
}

func main() {
	println(convert("PAYPALISHIRING", 2))
	println(convert("PAYPALISHIRING", 3))
	println(convert("PAYPALISHIRING", 4))
}
